package com.hmkj.core.mapper.operate;


import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.operate.AnnounceModel;
import com.hmkj.core.po.operate.Announce;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 公告相关Dao接口<br>
 *
 * @author Vector
 * @date   2017-06-16 09:59:49
 */
@Repository
public interface AnnounceMapper extends GenericMapper<Announce,Integer> {

    @Select("SELECT t.*,agent.name AS 'agent.name' FROM biz_announce t LEFT JOIN biz_agent agent ON agent.id = t.agent_id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AnnounceModel> findListBySqlJoin(Map<String, Object> params);

    @Select("SELECT count(1) FROM biz_announce t LEFT JOIN biz_agent agent ON agent.id = t.agent_id WHERE 1=1 ${whereSql}")
    long countBySqlJoin(Map<String, Object> params);

    /**
     * 前台获取公告列表
     * @param params
     * @return
     */
    @Select(" <script> SELECT COUNT(1) FROM biz_announce t WHERE t.del_flag = 1 AND t.status = #{status}" +
            "<if test=\"agentId != null and agentId != '' \"> AND t.`agent_id` in (0,#{agentId}) </if>" +
            "<if test=\"agentId == null or agentId == '' \"> AND t.`agent_id` in (0) </if>" +
            " ORDER BY t.release_time DESC </script>")
    Integer findCountForAnnounceList(Map<String, Object> params);

    /**
     * 前台获取公告列表
     */
    @Select(" <script> SELECT t.* FROM biz_announce t WHERE t.del_flag = 1 AND t.status = #{status}" +
            "<if test=\"agentId != null and agentId != '' \"> AND t.`agent_id` in (0,#{agentId}) </if>" +
            "<if test=\"agentId == null or agentId == '' \"> AND t.`agent_id` in (0) </if>" +
            " ORDER BY t.release_time DESC </script>")
    List<Announce> findListForAnnounceList(Map<String, Object> params);

    /**
     * 查询最新的announce
     * @param announce
     * @return
     */
    Announce findLatestAnnounce(Announce announce);

    /**
     * 统计用户未读公告
     * @param announceModel
     * @return
     */
    Integer countNotReadAnnounceNum(AnnounceModel announceModel);
}